<?php

/**
 * Created by Sven.
 * Date: 2016/12/29
 * Time: 16:50
 */
class ContentAction extends Action {

    public function __construct( $_tpl ) {
        $_model = new ContentModel();
        parent::__construct( $_tpl, $_model );
    }

    public function action() {
        if ( !isset( $_GET[ 'action' ] ) ) {
            $_GET[ 'action' ] = 'show';
        }

        switch ( $_GET[ 'action' ] ) {
            case 'add':
                $this->add();
                break;
            case 'delete':
                $this->delete();
                break;
            case 'update':
                $this->update();
                break;
            case 'show':
                $this->show();
                break;
            default:
                exit( 'ERROR：action参数非法' );
                break;
        }
    }

    private function add() {
        if ( IS_POST ) {
            $this->getPost();

            if ( $this->model->addContent() ) {
                Tool::alertLocation( '发布文档成功', '?action=add' );
            } else {
                Tool::alertBack( '警告：发布文档失败' );
            }

        }

        $this->tpl->assign( 'title', '新增文档' );
        $this->tpl->assign( 'add', true );

        // 显示分类
        $this->showNavs();
        // 显示排序
        $this->assignSorts();
        // 显示阅读权限
        $this->assignReadLimit();
    }

    // 显示分类
    private function showNavs( $_is_admin = false ) {
        $_nav_model = new NavModel();
        $_nav_model->pid = 0;
        $_all_navs = $_nav_model->getAllNavs();
        if ( $_all_navs ) {
            $_html = '';
            foreach ( $_all_navs as $_object ) {
                if ( $_is_admin ) {
                    $_html .= "<option value='{$_object->id}'>{$_object->nav_name}</option>";
                } else {
                    $_html .= "<optgroup label='{$_object->nav_name}'>\r\n";
                }

                $_nav_model->pid = $_object->id;
                $_nav_child_navs = $_nav_model->getAllChildNavs();
                if ( $_nav_child_navs ) {
                    foreach ( $_nav_child_navs as $_object_child ) {
                        $_html .= "<option value='{$_object_child->id}'>&nbsp;&nbsp;{$_object_child->nav_name}</option>\r\n";
                    }
                }

                if ( !$_is_admin ) {
                    $_html .= "</optgroup>\r\n";
                }

            }

        }

        $this->tpl->assign( 'all_navs', $_html );
    }

    private function delete() {
        if ( isset( $_GET[ 'id' ] ) ) {
            $_id = intval( $_GET[ 'id' ] );
            if ( $_id > 0 ) {
                $_affected_rows = $this->model->delete( 'cms_content', 'id', $_id );
                if ( $_affected_rows > 0 ) {
                    Tool::alertBack( '删除成功' );
                } else {
                    Tool::alertBack( '删除失败' );
                }
            } else {
                Tool::alertBack( '警告：非法的ID值' );
            }
        } else {
            Tool::alertBack( '警告：非法的ID值' );
        }
    }

    private function show() {
        $_nav = 0;
        if ( isset( $_GET[ 'nav_id' ] ) && intval( $_GET[ 'nav_id' ] ) > 0 ) {
            // 有nav_id，显示该nav_id 下的所有文章
            $_nav = intval( $_GET[ 'nav_id' ] );
        }

        $this->model->nav = $_nav;
        $_total = $this->model->getListCount(); // 自动给in赋值了（TODO: 这样做有瑕疵）
        $this->page( $_total, PAGE_SIZE ); // 执行完后，model的limit就有值了
        $_all_list = $this->model->getAllSubContent2();

        $this->tpl->assign( 'AllList', $_all_list );

//        var_dump($_all_list);

        $this->tpl->assign( 'title', '文档列表' );
        $this->tpl->assign( 'show', true );

        $this->showNavs( true );

    }

    private function update() {
        if ( IS_POST ) {
            if ( isset( $_POST[ 'id' ] ) ) {
                $_id = intval( $_POST[ 'id' ] );
                if ( $_id > 0 ) {
                    $this->getPost();

                    if ( $this->model->updateContent() ) {
                        Tool::alertLocation( '更新文档成功', $_POST[ 'prev_url' ] ? : '?action=show' );
                    } else {
                        Tool::alertBack( '警告：更新文档失败' );
                    }

                } else {
                    Tool::alertBack( '警告：非法的ID值' );
                }
            } else {
                Tool::alertBack( '警告：非法的ID值' );
            }

        }

        if ( isset( $_GET[ 'id' ] ) ) {
            $_id = intval( $_GET[ 'id' ] );
            if ( $_id > 0 ) {
                $_row = $this->model->getOne( 'cms_content', 'id', $_id );
                if ( $_row != null && is_object( $_row ) ) {
                    $this->tpl->assign( 'data', $_row );
                } else {
                    Tool::alertBack( '警告：未找到对应的文章' );
                }
            } else {
                Tool::alertBack( '警告：非法的ID值' );
            }
        } else {
            Tool::alertBack( '警告：非法的ID值' );
        }

        $this->tpl->assign( 'prev_url', PREV_URL );
        $this->tpl->assign( 'title', '修改文档' );
        $this->tpl->assign( 'update', '修改文档' );

        // 显示分类
        $this->showNavs();
        // 显示排序
        $this->assignSorts();
        // 显示阅读权限
        $this->assignReadLimit();
    }

    private function assignSorts() {
        $_sorts = array( 0 => '默认排序', 1 => '置顶一天', 2 => '置顶一周', 3 => '置顶一月', 4 => '置顶一年' );
        $_html = '';
        foreach ($_sorts as $_key => $_val){
            $_html .= "<option value='$_key'>$_val</option>";
        }
        $this->tpl->assign('sort_html', $_html);
    }

    private function assignReadLimit() {
        $_read_limits = array( 0 => '开放浏览', 1 => '初级会员', 2 => '中级会员', 3 => '高级会员', 4 => 'VIP会员' );
        $_html = '';
        foreach ($_read_limits as $_key => $_val){
            $_html .= "<option value='$_key'>$_val</option>";
        }
        $this->tpl->assign('limit_html', $_html);
    }


    // 将add update 操作时的数据验证和获取都到此方法中
    private function getPost() {
        // 标题title必填，且长度在2到25之间
        if ( Validate::checkMinLengh( $_POST[ 'title' ], 2 ) || Validate::checkMaxLengh( $_POST[ 'title' ], 25 ) ) Tool::alertBack( '标题长度必须在到25之间' );
        // 分类nav，必须为大于0的整数
        if ( Validate::checkPositiveInteger( $_POST[ 'nav' ] ) ) Tool::alertBack( '分类选择不正确' );
        // 标签tag，长度小于30
        if ( !Validate::checkNull( $_POST[ 'tag' ] ) ) {
            if ( Validate::checkMaxLengh( $_POST[ 'tag' ], 30 ) ) Tool::alertBack( '标签长度必须小于30' );
        }
        // 关键字keyword，长度小于30
        if ( !Validate::checkNull( $_POST[ 'keyword' ] ) ) {
            if ( Validate::checkMaxLengh( $_POST[ 'keyword' ], 30 ) ) Tool::alertBack( '关键字长度必须小于30' );
        }

        // 文章来源source，长度小于20
        if ( !Validate::checkNull( $_POST[ 'source' ] ) ) {
            if ( Validate::checkMaxLengh( $_POST[ 'source' ], 20 ) ) Tool::alertBack( '文章来源长度必须小于20' );
        }
        // 作者author，长度小于10
        if ( !Validate::checkNull( $_POST[ 'author' ] ) ) {
            if ( Validate::checkMaxLengh( $_POST[ 'author' ], 10 ) ) Tool::alertBack( '作者长度必须小于10' );
        }
        // 内容摘要info，长度小于10
        if ( !Validate::checkNull( $_POST[ 'info' ] ) ) {
            if ( Validate::checkMaxLengh( $_POST[ 'info' ], 200 ) ) Tool::alertBack( '内容摘要长度必须小于200' );
        }
        // 内容content, 不能为空
        if ( Validate::checkNull( $_POST[ 'content' ] ) ) Tool::alertBack( '内容不能为空' );
        // 浏览次数count，必须是正整数
        if ( !Validate::checkNull( $_POST[ 'count' ] ) ) {
            if ( Validate::checkPositiveInteger( $_POST[ 'count' ] ) ) {
                Tool::alertBack( ' 浏览次数必须是正整数' );
            }
        }
        // 消费金币必须是正整数
        if ( !Validate::checkNull( $_POST[ 'gold' ] ) ) {
            if ( Validate::checkPositiveInteger( $_POST[ 'gold' ], true ) ) {
                Tool::alertBack( ' 消费金币必须是整数（含0）' );
            }
        }

        $this->model->id = $_POST[ 'id' ];
        $this->model->title = $_POST[ 'title' ];
        $this->model->nav = $_POST[ 'nav' ];

        if ( isset( $_POST[ 'attr' ] ) && is_array( $_POST[ 'attr' ] ) ) {
            $_attr = implode( ',', $_POST[ 'attr' ] );
            $this->model->attr = $_attr;
        }
        $this->model->tag = $_POST[ 'tag' ];
        $this->model->keyword = $_POST[ 'keyword' ];
        $this->model->thumbnail = $_POST[ 'thumbnail' ];
        $this->model->source = $_POST[ 'source' ];
        $this->model->author = $_POST[ 'author' ];
        $this->model->info = $_POST[ 'info' ];
        $this->model->content = $_POST[ 'content' ];
        $this->model->commend = $_POST[ 'commend' ];
        $this->model->count = $_POST[ 'count' ];
        $this->model->gold = $_POST[ 'gold' ];
        $this->model->color = $_POST[ 'color' ];

        $this->model->sort = $_POST['sort'];
        $this->model->readlimit = $_POST['readlimit'];
    }


}